package cn.itcast.order.service;

import cn.itcast.feign.clients.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.用Feign远程调用
        User user = userClient.findById(order.getUserId());
        //将返回来的实体对象，交给order表中的User字段。
        order.setUser(user);
        // 4.返回
        return order;
    }
    @SentinelResource("goods")
    public void queryGoods(){
        System.err.println("查询商品信息.....");
    }



/*    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.利用RestTemplate发起http请求，查询用户
        //2.1 URL路径.  getId是Order表中的user_id 与用户表中的id相对应
//        String url = "http://localhost:8081/user/" + order.getUserId();
        String url = "http://userserver:8081/user/" + order.getUserId();
        //2.2返回的是JSON类型， 但是需要得到User对象类型。反序列化成对象类型
        User user = restTemplate.getForObject(url, User.class);
        //将返回来的实体对象，交给order表中的User字段。
        order.setUser(user);
        // 4.返回
        return order;
    }*/
}
